<?php
/*
	地区代理管理
*/
defined('BY_JYA') or exit('error');
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list_area','title'=>'地区代理'),
	array('op'=>'list_common','title'=>'普通代理'),
	array('op'=>'list_check','title'=>'审核代理'),
	array('op'=>'add_agent','title'=>'添加代理'),
);
$return['statuss']=array(
		'0'=>'待审核',
		'1'=>'审核通过',
		'2'=>'审核不通过',
);
$return['stops']=array(
		'0'=>'正常',
		'1'=>'禁用',
);
$return['types']=$this->agent_types();
if($_RQ['op']=='list'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$ids=$this->get_agent_team($_SESSION['uid']);
	$params['id']=$ids;
	//$params['type >']=0;
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['type']){
		$params['type']=0;
		$return['params']['type']=$_RQ['type'];
	}else{
		$params['type >']=0;
		$return['params']['type']=0;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 100;
	$total='';
	$fields=array();
	$list=pdo_getslice('jy_move_agentcar_agent',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['agent_area']=pdo_getcolumn('core_area',array('code'=>$item['area_code']),'address');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'代理商信息','type'=>'member'),
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'types'),
			array('name'=>'agent_area','title'=>'代理地区','type'=>'string'),
			array('name'=>'status','title'=>'审核状态','type'=>'radio','options'=>'statuss'),
			array('name'=>'stop','title'=>'是否禁止','type'=>'radio','options'=>'stops'),
			array('name'=>'createtime','title'=>'成为代理商时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'post','op'=>'stop','title'=>'禁止','show'=>array('stop'=>0)),
			array('type'=>'post','op'=>'start','title'=>'启用','show'=>array('stop'=>1)),
			array('type'=>'modal','op'=>'change','title'=>'调整代理地区'),
		)),
		
	);
	if($_SESSION['os']=='windows'){
		$return['htmls']=$htmls;
	}
	set_html('tabs',$tabs);
	exi($return);
}
if($_RQ['op']=='list_area'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$ids=$this->get_agent_team($_SESSION['uid']);
	$params['id']=$ids;
	$params['type >']=0;
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 100;
	$total='';
	$fields=array();
	$list=pdo_getslice('jy_move_agentcar_agent',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['agent_area']=pdo_getcolumn('core_area',array('code'=>$item['area_code']),'address');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'代理商信息','type'=>'member'),
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'types'),
			array('name'=>'agent_area','title'=>'代理地区','type'=>'string'),
			array('name'=>'status','title'=>'审核状态','type'=>'radio','options'=>'statuss'),
			array('name'=>'stop','title'=>'是否禁止','type'=>'radio','options'=>'stops'),
			array('name'=>'createtime','title'=>'成为代理商时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'post','op'=>'stop','title'=>'禁止','show'=>array('stop'=>0)),
			array('type'=>'post','op'=>'start','title'=>'启用','show'=>array('stop'=>1)),
			array('type'=>'modal','op'=>'change','title'=>'调整代理地区'),
		)),
		
	);
	if($_SESSION['os']=='windows'){
		$return['htmls']=$htmls;
	}
	set_html('tabs',$tabs);
	exi($return);
}
if($_RQ['op']=='list_common'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$params['agent_id']=$_SESSION['uid'];
	$params['type']=0;
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 100;
	$total='';
	$fields=array();
	$list=pdo_getslice('jy_move_agentcar_agent',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['agent_area']=pdo_getcolumn('core_area',array('code'=>$item['area_code']),'address');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'代理商信息','type'=>'member'),
			//array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'types'),
			array('name'=>'agent_area','title'=>'代理地区','type'=>'string'),
			array('name'=>'createtime','title'=>'成为代理商时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'post','op'=>'stop','title'=>'禁止','show'=>array('stop'=>0)),
			array('type'=>'post','op'=>'start','title'=>'启用','show'=>array('stop'=>1)),
			array('type'=>'modal','op'=>'change','title'=>'调整代理地区'),
		)),
		
	);
	if($_SESSION['os']=='windows'){
		$return['htmls']=$htmls;
	}
	set_html('tabs',$tabs);
	exi($return);
}
//审核代理商
if($_RQ['op']=='list_check'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$ids=$this->get_agent_team($_SESSION['uid']);
	$params['id']=$ids;
	$params['status']=0;
	$pindex = max(1, intval($_RQ['page']));
	$psize = 100;
	$total='';
	$fields=array();
	$list=pdo_getslice('jy_move_agentcar_agent',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['agent_area']=pdo_getcolumn('core_area',array('code'=>$item['area_code']),'address');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'代理商信息','type'=>'member'),
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'types'),
			array('name'=>'agent_area','title'=>'申请代理地区','type'=>'string'),
			array('name'=>'createtime','title'=>'申请时间','type'=>'time'),
		),'action'=>array(
			
			array('type'=>'modal','op'=>'check','title'=>'审核'),
		)),
		
	);
	if($_SESSION['os']=='windows'){
		$return['htmls']=$htmls;
	}
	set_html('tabs',$tabs);
	exi($return);
}
//审核代理商
if($_RQ['op']=='check'){
	
	if($_RQ['postdata']){
		$post=get_postdata();
		$update=array(
			'status'=>intval($post['status']),
		);
		$this->update('agent',$update,array('id'=>intval($post['id'])));
		exi('审核成功！');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择代理商！','error');
	}
	$item=$this->get('agent',array('id'=>$id));
	$item['nickname']=pdo_getcolumn('core_members',array('id'=>$item['mid']),'nickname');
	$item['address']=pdo_getcolumn('core_area',array('code'=>$item['area_code']),'address');
	$htmls=array(
		'type'=>'edit',
		'desc'=>'审核代理商',
		'edit'=>array(
			array('type'=>'string','name'=>'nickname','title'=>'代理商昵称','read'=>true),
			array('type'=>'string','name'=>'address','title'=>'代理地区','read'=>true),
			array('type'=>'radio','name'=>'status','title'=>'审核','options'=>'statuss'),
		),
	);

	$return['areas']=$this->get_agent_areas($_SESSION['uid']);
	if($_SESSION['os']=='windows'){
		$return['htmls']=$htmls;
	}
	$return['item']=$item;
	set_html('tabs',$tabs);
	exi($return);
}
//添加代理商，通过账号查找的方式添加代理商
if($_RQ['op']=='add_agent'){
	
	if($_RQ['postdata']){
		$post=get_postdata();
		
		if(!$post['username']){
			exi('请先选择用户！','error');
		}
		$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'username'=>trim($post['username'])),array('id'));
		if(!$member){
			exi('指定用户不存在！','error');
		}
		if($post['pcd']){
			if($post['pcd']['province']){
				//省级代理
				$code=pdo_getcolumn('core_area',array('type'=>1,'title'=>$post['pcd']['province']),'code');
				$type=1;
			}
			if($post['pcd']['city']){
				$code=pdo_getcolumn('core_area',array('type'=>2,'parent_code'=>$code,'title'=>$post['pcd']['city']),'code');
				$type=2;
			}
			if($post['pcd']['district']){
				$code=pdo_getcolumn('core_area',array('type'=>3,'parent_code'=>$code,'title'=>$post['pcd']['district']),'code');
				$type=3;
			}
			$check=$this->getcolumn('agent',array('uniacid'=>$_SESSION['uniacid'],'area_code'=>$code),'id');
			if($check){
				exi('该地区已存在代理商，请选择其他地区！','error');
			}
			$data['type']=$type;
			$data['area_code']=$code;
		}
		$new_agent=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$member['id'],
			'agent_id'=>$_SESSION['uid'],
			'createtime'=>TIMESTAMP,
			'type'=>$type,
			'status'=>1,
			'area_code'=>$code,
		);
		$this->insert('agent',$new_agent);
		
		exi('添加下级代理成功！','','list_area');
	}
	$htmls=array(
		'type'=>'edit',
		'desc'=>'添加下级代理',
		'edit'=>array(
			array('type'=>'string','name'=>'username','title'=>'用户账号'),
			array('type'=>'pcd','name'=>'pcd','title'=>'设置代理地区','options'=>'areas','help'=>'如果不指定代理地区，默认为普通代理'),
		),
	);

	$return['areas']=$this->get_agent_areas($_SESSION['uid']);
	if($_SESSION['os']=='windows'){
		$return['htmls']=$htmls;
	}
	
	$return['item']=array(
		'username'=>'',
	);
	set_html('tabs',$tabs);
	exi($return);
}

if($_RQ['op']=='change'){
	//调整代理地区
	
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('请先选择代理商！','error');
		}
		$data=array();
		if($post['pcd']){
			if($post['pcd']['province']){
				//省级代理
				$code=pdo_getcolumn('core_area',array('type'=>1,'title'=>$post['pcd']['province']),'code');
				$type=1;
			}
			if($post['pcd']['city']){
				$code=pdo_getcolumn('core_area',array('type'=>2,'parent_code'=>$code,'title'=>$post['pcd']['city']),'code');
				$type=2;
			}
			if($post['pcd']['district']){
				$code=pdo_getcolumn('core_area',array('type'=>3,'parent_code'=>$code,'title'=>$post['pcd']['district']),'code');
				$type=3;
			}
			$check=$this->getcolumn('agent',array('uniacid'=>$_SESSION['uniacid'],'area_code'=>$code),'id');
			if($check){
				exi('该地区已存在代理商，请选择其他地区！','error');
			}
			$data['type']=$type;
			$data['area_code']=$code;
		}
		if(!$data){
			exi('请先选择地区！','error');
		}
		$this->update('agent',$data,array('id'=>$id));
		exi('设置代理商成功！','','list_area');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要设置的代理商！','error');
	}
	$item=$this->get('agent',array('id'=>$id),array('id','area_code'));
	$htmls=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'pcd','name'=>'pcd','title'=>'设置代理地区','options'=>'areas'),
		),
	);
	$return['areas']=$this->get_agent_areas($_SESSION['uid']);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='stop'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要设置的代理商！','error');
	}
	$this->update('agent',array('stop'=>1),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('禁止成功！','','list_area');
}
if($_RQ['op']=='start'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要设置的代理商！','error');
	}
	$this->update('agent',array('stop'=>0),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('启用成功！','','list_area');
}
if($_RQ['op']=='del'){
	//删除代理商
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择代理商！','error');
	}
	$this->delete('agent',array('id'=>$id));
	exi('删除成功！','','list_area');
}